package org.lds.scheduled.job.admin.business.worker.converter;

import lombok.RequiredArgsConstructor;
import org.lds.scheduled.job.admin.business.app.entity.App;
import org.lds.scheduled.job.admin.business.app.mapper.AppMapper;
import org.lds.scheduled.job.admin.business.worker.entity.Worker;
import org.lds.scheduled.job.admin.business.worker.model.response.WorkerBaseResponse;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Objects;


@RequiredArgsConstructor
@Component
public class WorkerToBaseResponseConverter implements Converter<Worker, WorkerBaseResponse> {
    private final AppMapper appMapper;

    @Override
    public WorkerBaseResponse convert(Worker source) {
        App app = appMapper.selectById(source.getAppId());

        return WorkerBaseResponse.builder()
                .id(source.getId())
                .appId(source.getAppId())
                .appName(Objects.nonNull(app) ? app.getName() : null)
                .ip(source.getIp())
                .port(source.getPort())
                .enable(source.getEnable())
                .creator(source.getCreator())
                .modifier(source.getModifier())
                .createTime(source.getCreateTime())
                .updateTime(source.getUpdateTime())
                .build();
    }

}


